﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

/// <summary>
/// 显示/隐藏行为组件
/// </summary>
public class ShowHideBehaviour : MonoBehaviour {

    private CanvasGroup CanvasGroup;
    /// <summary>
    /// 透明度
    /// </summary>
    private float TargetAlpha;

    public void InitShowHide() {
        this.CanvasGroup = GetComponent<CanvasGroup>();
    }

    public void HandleShowHide() {
        UIHelper.HandleShowHide(this.CanvasGroup, this.TargetAlpha);
    }

    public void HandleShowHide(float smoothing) {
        UIHelper.HandleShowHide(this.CanvasGroup, this.TargetAlpha, smoothing);
    }

    public void HandleToast(float yieldSecond) {
        HandleShowHide();
        if (this.TargetAlpha == 1 && !IsInvoking()) {
            Invoke("Hide", yieldSecond);
        }
    }

    /// <summary>
    /// 显示
    /// </summary>
    public void Show() {
        this.TargetAlpha = 1;
        this.CanvasGroup.blocksRaycasts = true;
    }

    /// <summary>
    /// 隐藏
    /// </summary>
    public void Hide() {
        this.TargetAlpha = 0;
        this.CanvasGroup.blocksRaycasts = false;
    }

    /// <summary>
    /// 显示/隐藏切换
    /// </summary>
    public void Toggle() {
        if (this.TargetAlpha == 1) {
            this.Hide();
        } else {
            this.Show();
        }
    }

    /// <summary>
    /// 是否还在显示
    /// </summary>
    /// <returns></returns>
    public bool IsShowing() {
        return this.CanvasGroup.alpha > 0;
    }
}
